package com.ruoyi.web.wuchuan.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.result.BusinessException;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.common.core.result.ResultUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.wuchuan.enmu.ModuleTypeEnum;
import com.ruoyi.web.wuchuan.dto.PDFProperties;
import com.ruoyi.web.wuchuan.dto.ShapToJsonDTO;
import com.ruoyi.web.wuchuan.service.ConversionJsonService;
import com.ruoyi.web.wuchuan.util.ShapeFileUtils;
import com.ruoyi.web.wuchuan.vo.FileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @author by shixd
 * @date 2021/3/23
 * @desc
 */
@Controller
@RequestMapping("shap")
public class ConversionJsonController {

    @Autowired
    private ShapeFileUtils shapeFileUtils;

    @Autowired
    private ConversionJsonService conversionJsonService;




    @RequestMapping(value = "toJson", method = RequestMethod.POST)
    @ResponseBody
    public Result<ShapToJsonDTO> toJson(@RequestParam("file") MultipartFile file) throws IOException {
        String uuid = UUID.randomUUID().toString();
        FileVO fileVO = shapeFileUtils.saveFile(file,uuid);
        String shapPath = shapeFileUtils.zipFileRead(fileVO);
        ShapToJsonDTO shapToJsonDTO = shapeFileUtils.shape2Geojson(shapPath);
        shapeFileUtils.deleteFile(fileVO.getParentPath());
        return ResultUtils.success(shapToJsonDTO);
    }

//    @RequestMapping(value = "toShap",method = RequestMethod.POST)
//    @ResponseBody
//    public void toShap(@RequestBody @Validated JsonToShapDTO jsonToShapDTO, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String uuid = UUID.randomUUID().toString();
//        File file = shapeFileUtils.createFile(jsonToShapDTO.getShapName() + ".shp",uuid);
//        String shapPath = file.getPath();
//        shapeFileUtils.geojson2Shape(jsonToShapDTO.getJson(), shapPath);
//        List<String> pathList = new ArrayList<>();
//        pathList.add(file.getParentFile().getPath());
//        shapeFileUtils.zips(file.getParentFile().getPath() + File.separator + "矢量数据.zip", jsonToShapDTO.getShapName(), pathList);
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("multipart/form-data");
//        response.setHeader("Content-Disposition",
//                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, jsonToShapDTO.getShapName() + ".shp"));
//        FileUtils.writeBytes(file.getParentFile().getPath() + File.separator + "矢量数据.zip", response.getOutputStream());
//        shapeFileUtils.deleteFile(file.getParentFile().getPath());
//    }

    @RequestMapping(value = "toShapAndPDF",method = RequestMethod.POST)
    @ResponseBody
    public void toShapAndPDF(@RequestBody @Validated PDFProperties pdfProperties, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if(pdfProperties == null){
            throw new BusinessException("data is not be null");
        }
        ModuleTypeEnum moduleTypeEnum = ModuleTypeEnum.getByCode(pdfProperties.getType());
        if(moduleTypeEnum == null){
            throw new BusinessException("type is undefind");
        }
        File zipFile = conversionJsonService.downBeforeData(pdfProperties, moduleTypeEnum);
        String localPath = zipFile.getPath();
        // 下载名称
        String downloadName = zipFile.getName();
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type","application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(localPath, response.getOutputStream());
        shapeFileUtils.deleteFile(zipFile.getParentFile().getPath());
    }
}
